/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker;

import java.util.Collection;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
public class TrackerHelper {

    Session session = null;

    /**
     *
     */
    public TrackerHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public void closeThisSession() {
        this.session.close();
    }
//Torrents-------------------

    /**
     *
     * @param startID
     * @param endID
     * @return
     */
    public Collection getTorrentNames(int startID, int endID) {
        Collection<Torrents> torrentNameList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Torrents as tor where tor.torrentId between '" + startID + "' and '" + endID + "'");
            torrentNameList = (Collection<Torrents>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return torrentNameList;
    }

    /**
     *
     * @param userID
     * @param torrentType
     * @return
     */
    public Collection getTorrentOfUser(int userID, int torrentType) {
        Collection<Torrents> uploadTorrents = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            //Upload torrents whose torrentType = 1 | Seed torrents whose torrentType = 2 | Leech torrents whose torrentType = 3
            Query q = session.createQuery("from Points as point where point.users ='" + userID + "' and point.torrentType = '" + torrentType + "'");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadTorrents;
    }

    public Torrents getTorrentById(int id) {
        Torrents torrent = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Torrents as torrents where torrents.torrentId = " + id);
            torrent = (Torrents) q.uniqueResult();
        } catch (Exception e) {
        }
        return torrent;
    }

    public Collection getAllTorrents() {
        Collection<Torrents> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Torrents");
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public Collection getTorrentByName(String name) {
        Collection<Torrents> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Torrents as torrents where torrents.torrentName LIKE '" + name + "'");
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public void addTorrent(String torrentName, String torrentDescription, String torrentTime) {
        try {
            Torrents newTorrent = new Torrents(torrentName, torrentDescription, torrentTime);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.save(newTorrent);
            tx.commit();
        } catch (Exception e) {
        }
    }

//Comments-------------------
    /**
     *
     * @return
     */
    public Collection getComments() {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Comments");
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public Comments getCommentById(int id) {
        Comments newComment = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Comments as comments where comments.commentId = " + id);
            newComment = (Comments) q.uniqueResult();
        } catch (Exception e) {
        }
        return newComment;
    }

    /**
     *
     * @param id
     * @return
     */
    public Collection getCommentByTorrentID(int id) {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Comments as comments where comments.torrents.torrentId = " + id);
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public Collection getCommentByUserID(int id) {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Comments as comments where comments.users.userId = " + id);
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public void addComment(int torrentId, int userId, String content) {
        try {
            Comments newComment = new Comments(this.getTorrentById(torrentId), this.getUserById(userId), content);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.save(newComment);
            tx.commit();
        } catch (Exception e) {
        }
    }

    public void deleteComment(int id) {
        try {
            Comments newComment = this.getCommentById(id);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.delete(newComment);
            tx.commit();
        } catch (Exception e) {
        }
    }

    //Users--------
    public Collection getAllUsers() {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Users");
            list = q.list();

        } catch (Exception e) {
        }
        return list;
    }

    /**
     *
     * @param name
     * @return
     */
    public Collection getUserByName(String name) {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Users as users where users.userName LIKE '" + name + "'");
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public Users getUserById(int id) {
        Users user = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Users as users where users.userId = " + id);
            user = (Users) q.uniqueResult();
        } catch (Exception e) {
        }
        return user;
    }

    /**
     *
     * @param level
     * @return
     */
    public Collection getUserByLevel(Integer level) {
        Collection<Users> list = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Users as users where users.userLevel = " + level);
            list = q.list();
        } catch (Exception e) {
        }
        return list;
    }

    public void addUser(String username, String password) {
        try {
            Users newUser = new Users(username, password);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.save(newUser);
            tx.commit();
        } catch (Exception e) {
        }
    }

    public void deleteUserById(int id) {
        try {
            Users newUser = this.getUserById(id);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.delete(newUser);
            tx.commit();
        } catch (Exception e) {
        }
    }

    public void changeUserPassword(int id, String password) {
        try {
            Users newUser = this.getUserById(id);
            newUser.setPassword(password);
            org.hibernate.Transaction tx = session.beginTransaction();
            session.update(newUser);
            tx.commit();
        } catch (Exception e) {
        }
    }
}
